from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _
from djwheel.utils import trunc_text

class Config(models.Model):
    param = models.CharField(_('Parameter'), max_length=40, primary_key=True)
    description = models.CharField(_('Description'), max_length=120)
    value = models.TextField(_('Value'))

    class Meta:
        verbose_name = _('parameter')
        verbose_name_plural = _('parameters')

    def __unicode__(self):
        return self.param

    def adm_value(self):
        return trunc_text(self.value, 200)
    adm_value.short_description = value.verbose_name

    @classmethod
    def get(cls, param, default=''):
        try:
            return cls.objects.get(param=param).value
        except cls.DoesNotExist:
            return default
